using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VAPTXT
    {
        public DataRow Convert2APTXT(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryAPTXT(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["APTMANDANT"] = " ".PadRight(2 ); 
         dwReturn["APTAGANG"] = " ".PadRight(15 ); 
         dwReturn["APTLFDNR"] = " ".PadRight(4 ); 
         dwReturn["APTPRIO"] = " ".PadRight(1 ); 
         dwReturn["APTSPECTXT"] = " ".PadRight(1 ); 
         dwReturn["APTKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["APTPROGRP"] = " ".PadRight(3 ); 
         dwReturn["APTARTGRP"] = " ".PadRight(3 ); 
         dwReturn["APTWAKZ"] = " ".PadRight(1 ); 
         dwReturn["APTART"] = " ".PadRight(20 ); 
         dwReturn["APTVAR"] = " ".PadRight(3 ); 
         dwReturn["APTFARBE"] = " ".PadRight(10 ); 
         dwReturn["APTVDLART"] = " ".PadRight(4 ); 
         dwReturn["APTRKL"] = " ".PadRight(4 ); 
         dwReturn["APTMUKZ"] = " ".PadRight(1 ); 
         dwReturn["APTPOSTYP"] = " ".PadRight(3 ); 
         dwReturn["APTKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["APTKST"] = " ".PadRight(4 ); 
         dwReturn["APTMTYP"] = " ".PadRight(8 ); 
         dwReturn["APTMNRKRS"] = " ".PadRight(2 ); 
         dwReturn["APTMLFDNR"] = " ".PadRight(4 ); 
         dwReturn["APTMPOS"] = " ".PadRight(2 ); 
         dwReturn["APTEVARHNR"] = " ".PadRight(2 ); 
         dwReturn["APTEVARUNR"] = " ".PadRight(2 ); 
         dwReturn["APTVALDAT"] = " ".PadRight(0 ); 
         dwReturn["APTFILLER"] = " ".PadRight(111 ); 
         dwReturn["APTAENZL"] = " ".PadRight(0 ); 
         dwReturn["APTAENB"] = " ".PadRight(8 ); 
         dwReturn["APTAEND"] = " ".PadRight(0 ); 
         dwReturn["APTNEUB"] = " ".PadRight(8 ); 
         dwReturn["APTNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapAPTXT(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveAPTXT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryAPTXT(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapAPTXT(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertAPTXT(dwLocal, ITXDBVERSION);
            }
            UpdateAPTXT(dwLocal);
        }
        public void InsertAPTXT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO APTXT(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryAPTXT(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM APTXT WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM APTXT  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "APTXT";
            Return = Result;
            return Return;
        }
        public void DeleteAPTXT(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM APTXT   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateAPTXT(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE APTXT SET "
                    +"APTMANDANT= :APTMANDANT,"
                    +"APTAGANG= :APTAGANG,"
                    +"APTLFDNR= :APTLFDNR,"
                    +"APTPRIO= :APTPRIO,"
                    +"APTSPECTXT= :APTSPECTXT,"
                    +"APTKUNDNR= :APTKUNDNR,"
                    +"APTPROGRP= :APTPROGRP,"
                    +"APTARTGRP= :APTARTGRP,"
                    +"APTWAKZ= :APTWAKZ,"
                    +"APTART= :APTART,"
                    +"APTVAR= :APTVAR,"
                    +"APTFARBE= :APTFARBE,"
                    +"APTVDLART= :APTVDLART,"
                    +"APTRKL= :APTRKL,"
                    +"APTMUKZ= :APTMUKZ,"
                    +"APTPOSTYP= :APTPOSTYP,"
                    +"APTKSTKRS= :APTKSTKRS,"
                    +"APTKST= :APTKST,"
                    +"APTMTYP= :APTMTYP,"
                    +"APTMNRKRS= :APTMNRKRS,"
                    +"APTMLFDNR= :APTMLFDNR,"
                    +"APTMPOS= :APTMPOS,"
                    +"APTEVARHNR= :APTEVARHNR,"
                    +"APTEVARUNR= :APTEVARUNR,"
                    +"APTVALDAT= :APTVALDAT,"
                    +"APTFILLER= :APTFILLER,"
                    +"APTAENZL= :APTAENZL,"
                    +"APTAENB= :APTAENB,"
                    +"APTAEND= :APTAEND,"
                    +"APTNEUB= :APTNEUB,"
                    +"APTNEUD= :APTNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("APTMANDANT", OracleType.NVarChar).Value = dwData["APTMANDANT"].ToString(); 
          or1.Parameters.Add("APTAGANG", OracleType.NVarChar).Value = dwData["APTAGANG"].ToString(); 
          or1.Parameters.Add("APTLFDNR", OracleType.NVarChar).Value = dwData["APTLFDNR"].ToString(); 
          or1.Parameters.Add("APTPRIO", OracleType.NVarChar).Value = dwData["APTPRIO"].ToString(); 
          or1.Parameters.Add("APTSPECTXT", OracleType.NVarChar).Value = dwData["APTSPECTXT"].ToString(); 
          or1.Parameters.Add("APTKUNDNR", OracleType.NVarChar).Value = dwData["APTKUNDNR"].ToString(); 
          or1.Parameters.Add("APTPROGRP", OracleType.NVarChar).Value = dwData["APTPROGRP"].ToString(); 
          or1.Parameters.Add("APTARTGRP", OracleType.NVarChar).Value = dwData["APTARTGRP"].ToString(); 
          or1.Parameters.Add("APTWAKZ", OracleType.NVarChar).Value = dwData["APTWAKZ"].ToString(); 
          or1.Parameters.Add("APTART", OracleType.NVarChar).Value = dwData["APTART"].ToString(); 
          or1.Parameters.Add("APTVAR", OracleType.NVarChar).Value = dwData["APTVAR"].ToString(); 
          or1.Parameters.Add("APTFARBE", OracleType.NVarChar).Value = dwData["APTFARBE"].ToString(); 
          or1.Parameters.Add("APTVDLART", OracleType.NVarChar).Value = dwData["APTVDLART"].ToString(); 
          or1.Parameters.Add("APTRKL", OracleType.NVarChar).Value = dwData["APTRKL"].ToString(); 
          or1.Parameters.Add("APTMUKZ", OracleType.NVarChar).Value = dwData["APTMUKZ"].ToString(); 
          or1.Parameters.Add("APTPOSTYP", OracleType.NVarChar).Value = dwData["APTPOSTYP"].ToString(); 
          or1.Parameters.Add("APTKSTKRS", OracleType.NVarChar).Value = dwData["APTKSTKRS"].ToString(); 
          or1.Parameters.Add("APTKST", OracleType.NVarChar).Value = dwData["APTKST"].ToString(); 
          or1.Parameters.Add("APTMTYP", OracleType.NVarChar).Value = dwData["APTMTYP"].ToString(); 
          or1.Parameters.Add("APTMNRKRS", OracleType.NVarChar).Value = dwData["APTMNRKRS"].ToString(); 
          or1.Parameters.Add("APTMLFDNR", OracleType.NVarChar).Value = dwData["APTMLFDNR"].ToString(); 
          or1.Parameters.Add("APTMPOS", OracleType.NVarChar).Value = dwData["APTMPOS"].ToString(); 
          or1.Parameters.Add("APTEVARHNR", OracleType.NVarChar).Value = dwData["APTEVARHNR"].ToString(); 
          or1.Parameters.Add("APTEVARUNR", OracleType.NVarChar).Value = dwData["APTEVARUNR"].ToString(); 
          or1.Parameters.Add("APTVALDAT", OracleType.NVarChar).Value = dwData["APTVALDAT"].ToString(); 
          or1.Parameters.Add("APTFILLER", OracleType.NVarChar).Value = dwData["APTFILLER"].ToString(); 
          or1.Parameters.Add("APTAENZL", OracleType.NVarChar).Value = dwData["APTAENZL"].ToString(); 
          or1.Parameters.Add("APTAENB", OracleType.NVarChar).Value = dwData["APTAENB"].ToString(); 
          or1.Parameters.Add("APTAEND", OracleType.NVarChar).Value = dwData["APTAEND"].ToString(); 
          or1.Parameters.Add("APTNEUB", OracleType.NVarChar).Value = dwData["APTNEUB"].ToString(); 
          or1.Parameters.Add("APTNEUD", OracleType.NVarChar).Value = dwData["APTNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
